import React from 'react'
// ===================================================================== Temp
import SearchForm from '#cpt/temp/SearchForm'
import Table from '#cpt/temp/Table'
import Controls from '#cpt/temp/Controls'
import TableControls from '#cpt/temp/TableControls'
import Confirm from '#cpt/temp/Confirm'
import AddEditModal from '#cpt/temp/AddEditModal'
// ===================================================================== global declare
const { $fn, $Lazy } = window
// ===================================================================== $Lazy
const Page 		= $Lazy.load(()=>import('#tp/page/page-container'))
const $cache 	= $Lazy.file(()=>import('#private/cache-api'))
const EditModal		= $Lazy.state(()=>import('./cpt/edit-modal'))
// ===================================================================== component
class Index extends React.Component{
	state = {
		data:[],
		pag: {},
		selectedKeys:[],
		selectedRows:[],
		// 搜索表单列表
		forms : [
			{ label: '容器名称',		name: 'item_name' },
			{ label: '容器编号',		name: 'item_code' },
			{ label: '上级名称',		name: 'pid', 			half:true,	type:'treeSelect',	checkable:false,	multiple:false,	loading:true, idStr: 'id' },
			{ label: '科室',			name: 'project_pid', 	half:true,  type: 'select',		idStr: 'uuid' },
			{ label: '岗位',			name: 'project_id', 	half:true, 	type: 'select' },
			// { label: '容器场景',		name: 'item_code',		type:'select',		loading:true,		idStr: 'code'},
		],
		// 提交表单列表
		formData : [
			{ label: '容器名称',			name: 'item_name', 		half:true, 	max: 25,		required:true},
			{ label: '容器编号',			name: 'item_code', 		half:true,	},
			// { label: '容器场景',			name: 'item_code', 		half:true,  type: 'select',	required:true, loading:true,	idStr:'code' },
			{ label: '上级名称',			name: 'pid', 			half:true,	type:'treeSelect',	checkable:false,	multiple:false,	loading:true, idStr: 'id', 
				onChange: ({value}) => {
					const { formData } = this.state
					// const flag = this.pid.some(v => v.value === value || value === undefined)
					const flag = (value === '' || value === undefined)
					if (flag) {
						this.formRef.setValue({ item_code: '' })
						$fn.disabledRows(formData, 'item_code', false)
					} else {
						$fn.disabledRows(formData, 'item_code', true)
					}
					this.setState({formData})
				}
			},
			{ label: '排序',				name: 'item_sort', 		half:true,	type:'number',		minValue: 1, },
			{ label: '科室岗位同步',		name: 'project_update', full:true,  type: 'checkbox' },
			{ label: '科室',				name: 'project_pid', 	half:true,  type: 'select', idStr: 'uuid', loading:true },
			{ label: '岗位',				name: 'project_id', 	half:true, 	type: 'select',	loading:true },
			{ label: '温度同步',			name: 'temp_update', 	full:true,  type: 'checkbox' },
			{ label: '最低温度',			name: 'start_temp', 	half:true,	type: 'number', 	max: 10 },
			{ label: '最高温度',			name: 'end_temp', 		half:true,	type: 'number', 	max: 10 },
			{ label: '位置编号',			name: 'path_code', 		half:true, },
			{ label: '位置描述',			name: 'path_remark', 	full:true, 	type: 'textarea' },
			{ label: '备注',				name: 'remark', 		full:true,	type: 'textarea' },
			// { label: '标本容量',			name: 'spec_count', 	half:true	},
			// { label: '行',				name: 'rows', 			half:true, 	type: 'number', required:true, minValue: 1 },
			// { label: '列',				name: 'columns', 		half:true, 	type: 'number', required:true, minValue: 1 },
			// { label: '最低温度',			name: 'start_temp', 	half:true,	type: 'number', required:true, max: 10 },
			// { label: '最高温度',			name: 'end_temp', 		half:true,	type: 'number', max: 10 },
			// { label: '保存时间(天)',		name: 'keep_time', 		half:true,	type: 'number', required:true, minValue: 1},
		]
	}
	
	componentDidMount(){
		this.getSelect()
		this.fetch()
	}
	
	// 获取下拉数据
	getSelect = () => {
		const { forms, formData } = this.state
	
		$cache.then( async f => {
			// const data = await f.getDicCache({ code:90000 })
			// // $fn.setRow( forms, 'project_name', await f.getCache({ api:'shi_yan_ke_shi' }) )
			// // 容器场景
			// $fn.setRow( forms, 'item_code', data )
			// $fn.setRow( formData, 'item_code', data )
			
			// 岗位
			const projectId = await f.getCache({ api: 'gang_wei' })
			$fn.setRow(formData, 'project_id',  projectId)
			$fn.setRow(forms, 'project_id',  projectId)
			// 科室
			const projectPid = await f.getCache({ api: 'ke_shi' })
			$fn.setRow(formData, 'project_pid', projectPid)
			$fn.setRow(forms, 'project_pid', projectPid)
			// 上级名称
			const pid = await f.getCache({ api:'sp-container-lis/select',refresh:true })
			$fn.setRow(forms, 'pid', pid)
			$fn.setRow(formData, 'pid', pid)
			
			this.setState({ forms, formData })
		})
	}
	
	// 弹窗下拉
	getModalSelect = (rows) => {
		const { formData } = this.state
		$cache.then( async f => {
			// 上级名称
			$fn.setRow(formData, 'pid', await f.getCache({ api:'sp-container-lis/select', param: {id: rows.id},  refresh:true }))
			
			// 判断编号是否可填
			const flag = rows.pid === undefined || rows.pid === ''
			if (flag) {
				$fn.disabledRows(formData, 'item_code', false)
			} else {
				$fn.disabledRows(formData, 'item_code', true)
			}
			
			this.setState({ formData })
		})
	}
	
	// paging
	fetch = () => {this.getSelect(); return $fn.fetch.call(this,'sp-container-lis/index')}
	
	// 表单
	cols = [
		{ title: '容器名称', 		dataIndex: 'item_name', 			width:300 },
		{ title: '编号', 			dataIndex: 'item_code', 			width:100 },
		{ title: '排序', 			dataIndex: 'item_sort', 			width:80 },
		{ title: '最低温度', 		dataIndex: 'start_temp', 			...$fn.table.num, render: text => text + ' ℃' },
		{ title: '最高温度', 		dataIndex: 'end_temp', 				...$fn.table.num, render: text => text + ' ℃' },
		{ title: '位置编号', 		dataIndex: 'path_code', 			width:100 },
		{ title: '位置描述', 		dataIndex: 'path_remark', 			width:150 },
		{ title: '科室', 			dataIndex: 'parent_project_name', 	width:100 },
		{ title: '岗位', 			dataIndex: 'project_name', 			width:100 },
		{ title: '备注', 			dataIndex: 'remark', 				width:150 },
		// { title: '容器场景', 		dataIndex: 'item_code_name', 		width:150 },
		{ title: '创建时间', 		dataIndex: 'created_at', 			width:150 },
		// { title: '标本容量', 		dataIndex: 'spec_count', 		...$fn.table.exp_num },
		// { title: '行', 				dataIndex: 'rows', 				width:150 },
		// { title: '列', 				dataIndex: 'columns', 			width:150 },
		// { title: '温度', 			dataIndex: 'temperature', 		...$fn.table.num },
		// { title: '保存时间(天)', 	dataIndex: 'keep_time', 		width:150 },
		// { title: '已存标本量', 		dataIndex: 'spec_num', 			width:150 },
		TableControls.controls.call(this,{ width: 60, align:'left' })
	]
	
	// 表格操作按钮
	table_controls = (text,rows) => {
		return [
			TableControls.edit.call(this, null, null, { rows,
				click: () => this.getModalSelect(rows)
			}),		//编辑
			// +enabled === 0 ? TableControls.pushTo.call(this, '查看', `container-modal?uuid=${id}` ) : null,		// 查看
		]
	}
	
	onDel = uuid => Confirm.call(this, '删除', 'sp-container-lis/delete', { param: [uuid], idStr:'ids', isKeys: !uuid })
	
	// 操作按钮
	controls = () => {
		return [
			// 新增
			Controls.add.call(this, {
				click: () => {
					// 设置默认排序 1
					this.formRef.setValue({item_sort: 1})
					// 获取所有下拉参数
					this.getModalSelect({pid: '', id: ''})
				}
			}),
			// 删除
			this.onDel(),
			// 设置温度
			Controls.click.call(this, '设置温度', () => {
				this.editModal.open({uuids: this.state.selectedKeys}, false)
			}, {isKeys: !this.state.selectedKeys.length}),
		]
	}
	
	// // 获取岗位
	// getJobs = value => {
	// 	window.$http.submit(null,'project-team/jobselect', { param: { uuid: value }, isPull:true }).then(data=>{
	// 		const { formData } = this.state
	// 		if($fn.hasArray(data)){
	// 			$cache.then( async f => {
	// 				// 岗位
	// 				$fn.setRow(formData, 'project_id', data)
	// 				$fn.getRow(formData,'project_id').p = '请选择岗位'
	// 				this.setState({ formData })
	// 			})
	// 		}
	// 	})
	// }
	
	render(){
		return (
			<>
				<Page title='容器场景列表' controls={this.controls()}>
					{/* 搜索 */}
					{ SearchForm.call(this) }
					{/* 表格 */}
					{ Table.call(this, {idStr: 'id', hasCurrent: false}) }
				</Page>
				{/* 添加 编辑 */}
				{AddEditModal.call(this,{title:'容器场景列表', labelWidth:'s6', width:800, api:'sp-container-lis/add', editApi:'sp-container-lis/edit'})}
				{/* 设置温度 */}
				<EditModal onRef={ref => this.editModal = ref} onRefresh={this.fetch} api='sp-container-lis/setting' />
			</>
		)
	}
}

export default Index